﻿using AhpilyServer;
using PokerServer.Cache;
using Protocol.Code;
using Protocol.Dto;
using System;

namespace PokerServer.Logic
{
    /// <summary>
    /// 账号处理的逻辑层
    /// </summary>
    public class AccountHandler : IHandler
    {
        /// <summary>
        /// 下层的数据缓存层
        /// </summary>
        AccountCache accountCache = Caches.Account;

        /// <summary>
        /// 账号模块处理用户下线的方法
        /// </summary>
        /// <param name="client"></param>
        public void OnDisconnect(ClientPeer client)
        {
            if (accountCache.IsOnline(client))
            {
                accountCache.Offline(client);
            }
        }


        /// <summary>
        /// 账号模块的接受消息方法
        /// </summary>
        /// <param name="client"></param>
        /// <param name="subCode"></param>
        /// <param name="value"></param>
        public void OnReceive(ClientPeer client, int subCode, object value)
        {
            SingleExecute.Instance.Execute(() =>
            {
                switch (subCode)
                {
                    case AccountCode.REGIST_CREQ:
                    {
                        //Console.WriteLine("消息机制没啥大问题");
                        AccountDto dto = value as AccountDto;
                        //Console.WriteLine(dto.Account);
                        //Console.WriteLine(dto.Password);

                        Regist(client, dto.Account, dto.Password);
                    }
                        break;
                    case AccountCode.LOGIN:
                    {
                        //Console.WriteLine("消息机制没啥大问题");
                        AccountDto dto = value as AccountDto;
                        //Console.WriteLine(dto.Account);
                        //Console.WriteLine(dto.Password);
                        Login(client, dto.Account, dto.Password);
                    }
                        break;
                    default:
                        Console.WriteLine("AccountHandler OnReceive Script:26");
                        return;
                }
            });
        }


        /// <summary>
        /// 注册（逻辑层）
        /// </summary>
        /// <param name="client"></param>
        /// <param name="account"></param>
        /// <param name="password"></param>
        private void Regist(ClientPeer client, string account, string password)
        {
            SingleExecute.Instance.Execute(() =>
            {
                if (accountCache.IsExist(account))
                {
                    // 表示账号已经存在
                    // client.Send(OpCode.ACCOUNT, AccountCode.REGIST_SRES, "账号已经存在");
                    client.Send(OpCode.ACCOUNT, AccountCode.REGIST_SRES, -1);
                    return;
                }

                if (string.IsNullOrEmpty(account))
                {
                    // 表示账号或密码输入不合法
                    //client.Send(OpCode.ACCOUNT, AccountCode.REGIST_SRES, "账号或密码输入不合法");
                    client.Send(OpCode.ACCOUNT, AccountCode.REGIST_SRES, -2);

                    return;
                }

                if (string.IsNullOrEmpty(password) || password.Length < 4 || password.Length > 16)
                {
                    // 表示密码长度不对
                    client.Send(OpCode.ACCOUNT, AccountCode.REGIST_SRES, -3);
                    //client.Send(OpCode.ACCOUNT, AccountCode.REGIST_SRES, "密码长度不对");
                    return;
                }

                // 可以注册了
                accountCache.Create(account, password);
                client.Send(OpCode.ACCOUNT, AccountCode.REGIST_SRES, 0);
                //client.Send(OpCode.ACCOUNT, AccountCode.REGIST_SRES, "注册成功");
            });
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="client"></param>
        /// <param name="account"></param>
        /// <param name="password"></param>
        private void Login(ClientPeer client, string account, string password)
        {
            SingleExecute.Instance.Execute(() =>
            {
                if (!accountCache.IsExist(account))
                {
                    // 表示账号不存在
                    //client.Send(OpCode.ACCOUNT, AccountCode.LOGIN, "账号不存在");
                    client.Send(OpCode.ACCOUNT, AccountCode.LOGIN, -1);

                    return;
                }

                if (accountCache.IsOnline(account))
                {
                    // 表示账号在线
                    client.Send(OpCode.ACCOUNT, AccountCode.LOGIN, -2);
                    //client.Send(OpCode.ACCOUNT, AccountCode.LOGIN, "账号在线");
                    return;
                }

                if (!accountCache.IsMatch(account, password))
                {
                    // 表示账号密码不匹配
                    client.Send(OpCode.ACCOUNT, AccountCode.LOGIN, -3);
                    //client.Send(OpCode.ACCOUNT, AccountCode.LOGIN, "密码表示账号密码不匹配长度不对");

                    return;
                }

                // 登录成功
                client.Send(OpCode.ACCOUNT, AccountCode.LOGIN, 0);
                //client.Send(OpCode.ACCOUNT, AccountCode.LOGIN, "登录成功");

                accountCache.Online(client, account);
            });
        }
    }
}